clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "log_salesg_real_1lag_std size_assets_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std ln_market_cap_q_1lag_std tobins_q_1lag_std lev_d2c_1lag_std dd_1lag_std age_1lag_std i.sector i.fqtr"

local agg_controls "lngdp inflation unemp nasdaq fedfunds"

*************
***Options***
*************
*Set ivol measure to use as baseline
local matur m1545
*Set mp shock to use as baseline
local mps ff4_d

* Set the LP details ========================================================================
*# periods to compute the responses foreach
local hor = 20
*# lags of "endogenous" variables 
local lagy = 4 
*# lags of "shock"
local lage = 0 

********************************************************************************
********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}Compustat_Quarterly_Final", clear

local n = 20

*Set panel data
xtset firmid ts_qtr

*Flag sample for those in at least n quarters
bysort gvkey pre_crisis: egen obs_num_temp = count(gvkey) if pre_crisis
bysort gvkey: egen obs_num = max(obs_num_temp)

*Flag outliers (bottom/top 0.5%) for investment
_pctile investment_intensive if pre_crisis, p(.5 99.5)
gen 	investment_outlier = (investment_intensive<r(r1)|investment_intensive>r(r2))

gen sample_period=(pre_crisis==1)

gen in_sample = (obs_num>=`n' & !missing(obs_num) & investment_outlier==0 & sample_period)

********************************************************************************
*Set panel data
xtset firmid ts_qtr

gen ivol_`matur'_std = ivol_`matur'
gen vix_eoq_std = vix_eoq
gen dd_std = dd
gen age_1lag_std = age

rename age age_1lag

***Standardize quarterly Compustat variables and lagged ivol level
foreach i in dd_1lag age_1lag log_salesg_real_1lag size_assets_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2c_1lag ivol_`matur' vix_eoq {

	*Pre-Crisis Standardization
	drop `i'_std
	qui: sum `i' if in_sample
	gen `i'_std = (`i' - r(mean)) / r(sd)
	
}

*Standardize ivol and dd
_pctile ivol_`matur' if in_sample, percentiles(30 70)
gen ivol_`matur'_hi2lo = (ivol_`matur' - r(r1)) / (r(r2) -  r(r1))

_pctile dd  if in_sample, percentiles(30 70)
gen dd_hi2lo = (dd - r(r1)) / (r(r2) -  r(r1))

*Standardize mps to unit standard deviation & positive is expansionary
gen `mps'=`mps'_shock_qtr
egen date_tag=tag(ts_qtr)

xtset
egen time=group(ts_qtr)
egen time_tag=tag(time)
xtset firmid time

gen ff4_d_toPlot=-ff4_d*100

egen ff4_d_rank=rank(ff4_d) if time_tag==1 & in_sample

*Exclude 5 expansionary shocks
gen ff4_d_outlier=ff4_d_toPlot if !missing(ff4_d_rank) & ff4_d_rank>=45

tab ff4_d_rank

gen zlb_dates = (year>=2009 & year<=2015)

twoway (bar ff4_d_toPlot ts_qtr if time_tag & year>=1996 & (zlb_dates), fcolor(black) color(black)) (bar ff4_d_toPlot ts_qtr if time_tag & year>=1996 & !(zlb_dates) & year<=2019, fcolor(white)), ytitle("FF4 (basis points)", size(medlarge)) xtitle(" ") xlabel(,labsize(medlarge) nogrid) ylabel(,labsize(medlarge) nogrid) legend(off)

graph export "${output}figureA8.pdf", replace
